package com.ktgj.customer.service.impl;

import com.ktgj.customer.common.CardConstant;
import com.ktgj.customer.domain.CouponBO;
import com.ktgj.customer.domain.dto.ActualPriceDetailed;
import com.ktgj.customer.domain.dto.CalculationDTO;
import com.ktgj.customer.domain.dto.DiscountDescDTO;
import com.ktgj.customer.domain.vo.CalculationVO;
import com.ktgj.customer.service.ICouponService;
import com.ktgj.customer.service.IUtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service
public class IUtilServiceImpl implements IUtilService {

    @Autowired
    private ICouponService couponService;

    @Override
    public CalculationDTO calculation(CalculationVO calculationVO) {
        CalculationDTO calculationDTO = new CalculationDTO();
        BigDecimal price = new BigDecimal(0);
        BigDecimal Negative = new BigDecimal(-1);

        for (BigDecimal item : calculationVO.getPrices()) {
            price = price.add(item);
        }
        price = price.multiply(new BigDecimal(calculationVO.getAmount().intValue()));//商品原价总额

        /*BigDecimal additionalPrice = new BigDecimal(0);
        //计算辅营产品价格,几个辅营计算几个价格
        if (calculationVO.getAdditionalPrices() != null && calculationVO.getAdditionalPrices().size() > 0) {
            for (BigDecimal item : calculationVO.getAdditionalPrices()) {
                additionalPrice = additionalPrice.add(item);
            }
//            if (calculationVO.getAdditionalAmount() != null && calculationVO.getAdditionalAmount() > 0)
//                additionalPrice = additionalPrice.multiply(new BigDecimal(calculationVO.getAdditionalAmount().intValue()));//附加商品原价总额
        }*/
        BigDecimal additionalPrice = new BigDecimal(0);
        if (calculationVO.getAdditionalPrices() != null && calculationVO.getAdditionalPrices().size() > 0) {
            for (BigDecimal item : calculationVO.getAdditionalPrices()) {
                additionalPrice = additionalPrice.add(item);
            }
            if (calculationVO.getAdditionalAmount() != null && calculationVO.getAdditionalAmount() > 0)
                additionalPrice = additionalPrice.multiply(new BigDecimal(calculationVO.getAdditionalAmount().intValue()));//附加商品原价总额
        }
        price = price.add(additionalPrice);//支付原价总价格

        ActualPriceDetailed actualPriceDetailed = new ActualPriceDetailed();
        actualPriceDetailed.setOriginalPrice(new BigDecimal(price.doubleValue()));

        List<DiscountDescDTO> discountDescDTOS = new ArrayList<>();
        BigDecimal savePrice = new BigDecimal(0);

        if (calculationVO.getCouponId() != null && calculationVO.getCouponId() != 0) {
            CouponBO coupon = couponService.couponByCustomerRightDetailId(calculationVO.getCouponId());
            if (coupon != null) {
                DiscountDescDTO discountDescDTO = new DiscountDescDTO();
                switch (coupon.getCouponType()) {
                    case "CouponTypeFullMinus":
                        if (actualPriceDetailed.getOriginalPrice().compareTo(coupon.getCouponMaxprice()) == -1)
                            break;
                    case "CouponTypeCashCoupon":
                        price = price.subtract(coupon.getCouponPrice());
                        discountDescDTO.setDesc("优惠券");
                        discountDescDTO.setPrice(coupon.getCouponPrice().multiply(Negative));
                        savePrice = savePrice.add(coupon.getCouponPrice());
                        break;
                    case "CouponTypeDiscount":
                        BigDecimal dec = price.multiply(coupon.getCouponDiscount());
                        price = price.subtract(dec);
                        discountDescDTO.setDesc("优惠券");
                        discountDescDTO.setPrice(dec.multiply(Negative));
                        savePrice = savePrice.add(dec);
                        break;
                }
                discountDescDTOS.add(discountDescDTO);
            }
        }

        if (CardConstant.EXCHANGE > calculationVO.getPointAmount()) {
            calculationDTO.setUsePointDesc("积分不足");
            calculationDTO.setSurplusPointDesc("需要" + CardConstant.EXCHANGE + "兑换￥1，您拥有" + calculationVO.getPointAmount() + "积分");
        } else {
            Integer dec = calculationVO.getPointAmount() / CardConstant.EXCHANGE;
            calculationDTO.setUsePointDesc("使用" + dec * CardConstant.EXCHANGE + "积分抵扣￥" + dec);
            calculationDTO.setSurplusPointDesc("剩余" + (calculationVO.getPointAmount() - (dec * CardConstant.EXCHANGE)) + "积分");

            if (calculationVO.isUsePoint()) {
                BigDecimal decimal = new BigDecimal(dec);
                price = price.subtract(decimal);
                DiscountDescDTO discountDescDTO = new DiscountDescDTO();
                discountDescDTO.setDesc("积分");
                discountDescDTO.setPrice(decimal.multiply(Negative));
                savePrice = savePrice.add(decimal);
                discountDescDTOS.add(discountDescDTO);
            }
        }

        if (calculationVO.isUseDeposit() && calculationVO.getDepositAmount() != null && !calculationVO.getDepositAmount().equals(0)) {
            DiscountDescDTO discountDescDTO = new DiscountDescDTO();
            discountDescDTO.setDesc("金币");
            if (calculationVO.getDepositAmount().compareTo(price) == 1) {
                discountDescDTO.setPrice(price.multiply(Negative));
                price = new BigDecimal(0);
            } else {
                discountDescDTO.setPrice(calculationVO.getDepositAmount().multiply(Negative));
                price = price.subtract(calculationVO.getDepositAmount());
            }
            discountDescDTOS.add(discountDescDTO);
        }

        actualPriceDetailed.setDiscountDesc(discountDescDTOS);
        actualPriceDetailed.setActualPrice(price);
        calculationDTO.setActualPriceDetailed(actualPriceDetailed);
        calculationDTO.setPrice(price);
        calculationDTO.setSavePrice(savePrice);

        return calculationDTO;
    }

}
